//
//  HTTPManagers.m
//  liren
//
//  Created by long on 16/3/8.
//  Copyright © 2016年 Jone. All rights reserved.
//

#import "HTTPManagers.h"
#import "AFHTTPSessionManager.h"
#import "AFNetworking.h"
#import "MVPhoto.h"

//开关、分享地址
#define  OpenbasePath @"http://hy8998.com"
//论坛地址
#define  BBSbasePath @"http://888666abc.com"
#define  basePath   @"http://appapi.kxt.com"
#define  kNBAFootBall   @"http://www.buyinball.com/forum/api/note"
#define  kForumUserurl  @"http://23.234.49.240/api/user"
#define  kForumLikeurl  @"http://23.234.49.240/api/topic"
#define  kForumCommentBaseurl @"http://23.234.49.240/api"
#define  kForumCommentAddurl @"http://23.234.49.240/api/comment"
@implementation HTTPManagers
#pragma mark - 一、POST请求

#pragma mark 1 请求数据
+ (void)postWithURL:(NSString *)urlStr
             params:(id)params
            success:(void (^)(id))success
            failure:(void (^)(NSError *))failure
{
    
    // 2 发送POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",urlStr,params);
    [manager POST:urlStr parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
    
}

#pragma mark 2 上传图片数据
+ (void)postWithURL:(NSString*)urlStr
             params:(NSDictionary*)params
      formImageName:(NSArray*)arrayImageName
          formImage:(NSArray*)arrayImage
            success:(void(^)(id json))success
            failure:(void(^)(NSError *error))failure
{
    // 1  创建请求管理员
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2  发送POST请求
    [manager POST:urlStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSLog(@"%@",params);
        for (NSInteger i=0; i<arrayImage.count; i++) {
            NSData *imageData = UIImageJPEGRepresentation(arrayImage[i], 0.7);
            [formData appendPartWithFileData:imageData name:arrayImageName[i] fileName:@"file.jpg" mimeType:@"image/jpg"];
        }
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
    

}

#pragma mark 3 上传base64图片数据
+ (void)postWithURL:(NSString*)urlStr
          formImage:(UIImage *)image
            success:(void(^)(id json))success
            failure:(void(^)(NSError *error))failure{
    NSString *imageBase64 = [BCPhoto image2String:image];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          imageBase64,@"img",
                          nil];
    // 1 创建请求管理员
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2  发送POST请求
    [manager POST:urlStr parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (success)
        {
            NSLog(@"%@",responseObject);
            success(responseObject);
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
    
}
#pragma mark - 二、 GET请求
+ (void)getWithURL:(NSString *)urlStr
            params:(NSDictionary *)params
           success:(void (^)(id))success
           failure:(void (^)(NSError *))failure
{

    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",basePath,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}
//live
+ (void)getWithLiveURL:(NSString *)urlStr
                params:(NSDictionary *)params
               success:(void (^)(id))success
               failure:(void (^)(NSError *))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",klivebaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}
+ (void)getWithJiJinURL:(NSString *)urlStr
                params:(NSDictionary *)params
               success:(void (^)(id))success
               failure:(void (^)(NSError *))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kJijinBaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}
+ (void)getWithRecommendURL:(NSString*)urlStr
                     params:(NSDictionary*)params
                    success:(void(^)(id json))success
                    failure:(void(^)(NSError *error))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kRecommendBaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}
+ (void)getWithHomeURL:(NSString*)urlStr
                     params:(NSDictionary*)params
                    success:(void(^)(id json))success
                    failure:(void(^)(NSError *error))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kHomebaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}
+ (void)getWithScoreURL:(NSString*)urlStr
                 params:(NSDictionary*)params
                success:(void(^)(id json))success
                failure:(void(^)(NSError *error))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kScoreBaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];

}
+ (void)getWithForum:(NSString*)urlStr
              params:(NSDictionary*)params
             success:(void(^)(id json))success
             failure:(void(^)(NSError *error))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kForumBaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];

}
+ (void)getWithOddsURL:(NSString*)urlStr
                params:(NSDictionary*)params
               success:(void(^)(id json))success
               failure:(void(^)(NSError *error))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kOddsBaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];

}
+ (void)getWithHomeVideo:(NSString*)urlStr
                  params:(NSDictionary*)params
                 success:(void(^)(id json))success
                 failure:(void(^)(NSError *error))failure{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kHomeVideourl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];

}
//BBS
+ (void)postBBSWithURL:(NSString*)urlStr
                params:(id)params
               success:(void(^)(id json))success
               failure:(void (^)(NSError *error))failure
{
    // 2 发送POST请求
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",BBSbasePath,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager POST:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
    
}
+ (void)postNBAFootBallWithURL:(NSString*)urlStr
                        params:(id)params
                       success:(void(^)(id json))success
                       failure:(void (^)(NSError *error))failure{
    // 2 发送POST请求
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kNBAFootBall,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager POST:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];

}

+ (void)getWithOpenURL:(NSString*)urlStr
                params:(NSDictionary*)params
               success:(void(^)(id json))success
               failure:(void(^)(NSError *error))failure
{
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",OpenbasePath,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}

//BBS
+ (void)getWithBBSURL:(NSString*)urlStr
               params:(NSDictionary*)params
              success:(void(^)(id json))success
              failure:(void(^)(NSError *error))failure
{
    
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",BBSbasePath,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager GET:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
    
    
}
+ (void)postForumUserWithURL:(NSString*)urlStr
                      params:(id)params
                     success:(void(^)(id json))success
                     failure:(void (^)(NSError *error))failure{
    // 2 发送POST请求
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kForumUserurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager POST:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];

}
+ (void)postLikeWithURL:(NSString*)urlStr
                 params:(id)params
                success:(void(^)(id json))success
                failure:(void (^)(NSError *error))failure{
    // 2 发送POST请求
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kForumLikeurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager POST:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}
+ (void)postCommentWithURL:(NSString*)urlStr
                 params:(id)params
                success:(void(^)(id json))success
                failure:(void (^)(NSError *error))failure{
    // 2 发送POST请求
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kForumCommentBaseurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager POST:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}
+ (void)postCommentAddWithURL:(NSString*)urlStr
                    params:(id)params
                   success:(void(^)(id json))success
                   failure:(void (^)(NSError *error))failure{
    // 2 发送POST请求
    NSString *newUrl = [NSString stringWithFormat:@"%@/%@",kForumCommentAddurl,urlStr];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"%@%@",newUrl,params);
    [manager POST:newUrl parameters:params?:@"" success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (success)
        {
            success(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if (failure)
        {
            failure(error);
        }
    }];
}

@end
